<?php
	require_once (dirname(__FILE__)."/../../config/Config.inc");
	require_once (dirname(__FILE__)."/../../config/api.inc");
	require_once ("load.php");
	
	$parm = requestArrayParm($_REQUEST);
	
	if(empty($parm['start']) || empty($parm['end']) || empty($parm['month']))
	{
		$result['status'] = '0';
		$result['msg'] = '缺少参数start和end,month';
		echoJSON($result);
		exit;
	}
	
	$result['status'] = '1';
	
	unset($wheres, $pages, $sorts,$between,$data);
	
	$between['start'] = strtotime($parm['start']);
	$between['end'] = strtotime($parm['end'])+86399;
	
	$wheres['userid'] = $userinfo['id'];
	
	$wheres['status'] = '2';
	
	$wheres['status2'] = '2';
	
	$wheres['endtime'] = array(">=",time());
	
	$sorts['fltime'] = 'asc';
	
	
	$result['zdsbj'] = _SqlTool::getSum('tzinfo', $wheres, array(), 'money');
	
	$result['zdslx'] = _SqlTool::getSum('tzinfo', $wheres, array(), 'lixi');
	
	unset($wheres['endtime']);
	
	$wheres['fltime'] = array(">",strtotime(date('Y-m-d')));
	
	$result['zdsfl'] = _SqlTool::getSum('tzinfo', $wheres, array(), 'fanli');
	
	unset($wheres['fltime']);

	$resData = _SqlTool::getList('tzinfo', $wheres, $pages, $sorts,$between,'fltime');
	
	unset($jrfl,$jyfl,$jrhk,$jyhk,$jrlx,$jylx,$temp);
	foreach ($resData as $key => $val)
	{
		$temp[date('Y-m-d',$val['fltime'])]['fanli'][] = $val['fanli'];
		if(date('Y-m-d') == date('Y-m-d',$val['fltime']))
		{
			$jrfl[] = $val['fanli'];
		}
		
		if(strtotime($parm['month']) == strtotime(date('Y-m',$val['fltime'])))
		{
			$jyfl[] = $val['fanli'];
		}
	}
	
	$resData = _SqlTool::getList('tzinfo', $wheres, $pages, $sorts,$between,'endtime');
	foreach ($resData as $key => $val)
	{
		$temp[date('Y-m-d',$val['endtime'])]['benjin'][] = $val['money'];
		$temp[date('Y-m-d',$val['endtime'])]['lixi'][] = $val['lixi'];
		if(date('Y-m-d') == date('Y-m-d',$val['endtime']))
		{
			$jrhk[] = $val['money'];
		}
		
		if(strtotime($parm['month']) == strtotime(date('Y-m',$val['endtime'])))
		{
			$jyhk[] = $val['money'];
		}
		
		if(date('Y-m-d') == date('Y-m-d',$val['endtime']))
		{
			$jrlx[] = $val['lixi'];
		}
		
		if(strtotime($parm['month']) == strtotime(date('Y-m',$val['endtime'])))
		{
			$jylx[] = $val['lixi'];
		}
		
	}
	
	ksort($temp);

	foreach ($temp as $key => $value)
	{
		unset($data);
		$data['date'] = $key;
		$data['flsum'] = round(floatval(array_sum($value['fanli'])),2);
		$data['bjsum'] = round(floatval(array_sum($value['benjin'])),2);
		$data['lxsum'] = round(floatval(array_sum($value['lixi'])),2);
		$data['hksum'] = $data['bjsum'] + $data['lxsum'];
		
		$data['flnum'] = count($value['fanli']);
		$data['bjnum'] = count($value['benjin']);
		$data['lxnum'] = count($value['lixi']);
		
		
		
		$result['datalist'][] = $data;
	}

	$result['jrhk'] = round(floatval(array_sum($jrhk)),2);
	
	$result['jyhk'] = round(floatval(array_sum($jyhk)),2);
	
	$result['jyhkbs'] = count($jyhk);
	
	$result['jrfl'] = round(floatval(array_sum($jrfl)),2);
	
	$result['jyfl'] = round(floatval(array_sum($jyfl)),2);
	
	$result['jyflbs'] = count($jyfl);
	
	$result['jrlx'] = round(floatval(array_sum($jrlx)),2);
	
	$result['jylx'] = round(floatval(array_sum($jylx)),2);
	
	$result['jylxbs'] = count($jylx);
	
	$result['sumjr'] = $result['jrfl'] + $result['jrhk'] + $result['jrlx'];
	
	$result['sumjy'] = $result['jyfl'] + $result['jyhk'] + $result['jylx'];
	
	echoJSON($result);